package com.kevin.hibernate.session;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.kevin.hibernate.dto.Person;

public class Session {

	private static String URL = "jdbc:mysql://localhost:3306/hibernate";
	private static String USER_NAME = "root";
	private static String PASSWORD = "root";

	private Map<String, String> propertyNameMay = new HashMap<String, String>();
	private String[] methodsName;

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Session() {

		propertyNameMay.put("_id", "id");
		propertyNameMay.put("_age", "age");
		propertyNameMay.put("_name", "name");

		methodsName = new String[propertyNameMay.size()];
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(URL, USER_NAME, PASSWORD);
	}

	public void save(Person person) throws ClassNotFoundException,
			SQLException, SecurityException, NoSuchMethodException,
			IllegalArgumentException, IllegalAccessException,
			InvocationTargetException {

		Connection conn = this.getConnection();

		String sql = this.createSql();

		PreparedStatement pst = conn.prepareStatement(sql);

		for (int i = 0; i < methodsName.length; i++) {
			Method method = person.getClass().getMethod(methodsName[i]);
			Class returnType = method.getReturnType();

			if (returnType.getName().equals("java.lang.String")) {
				String returnValue = (String) method.invoke(person);
				pst.setString(i + 1, returnValue);
			}

			if (returnType.getName().equals("int")) {
				int returnValue = (Integer) method.invoke(person);
				pst.setInt(i + 1, returnValue);
			}
		}

		pst.executeUpdate();
		pst.close();
		conn.close();

	}

	public String createSql() {

		String arg = "";
		String value = "";
		int index = 0;
		for (String key : this.propertyNameMay.keySet()) {

			String pro = this.propertyNameMay.get(key);

			arg += pro + ",";
			value += "?,";

			methodsName[index] = "get" + Character.toUpperCase(pro.charAt(0))
					+ pro.substring(1);

			index++;
		}

		arg = arg.substring(0, arg.length() - 1);
		value = value.substring(0, value.length() - 1);

		String sql = "insert into person(" + arg + ") values (" + value + ")";

		return sql;
	}

	public static void main(String[] args) throws SecurityException,
			IllegalArgumentException, ClassNotFoundException, SQLException,
			NoSuchMethodException, IllegalAccessException,
			InvocationTargetException {
		Session session = new Session();

		Person person = new Person();

		person.setId(10);
		person.setAge(25);
		person.setName("Tom");

		session.save(person);
	}
}
